﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Informant.Contacts;

namespace Informant.Services.Models.Contacts
{
	[DataContract(Name = "Contact")]
	public class ContactServiceModel : IContact
	{
		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		[DataMember]
		public String Id { get; set; }
		/// <summary>
		/// Gets or sets the first name.
		/// </summary>
		/// <value>The first name.</value>
		[DataMember]
		public String FirstName { get; set; }
		/// <summary>
		/// Gets or sets the last name.
		/// </summary>
		/// <value>The last name.</value>
		[DataMember]
		public String LastName { get; set; }
		/// <summary>
		/// Gets the full name.
		/// </summary>
		/// <value>The full name.</value>
		public String Name
		{
			get
			{
				return String.Format("{0} {1}", FirstName, LastName);
			}
			set
			{
				if (value.Contains(' '))
				{
					FirstName = value.Substring(0, value.IndexOf(' ') - 1);
					if (value.IndexOf(' ') != value.Length - 1)
					{
						LastName = value.Substring(value.IndexOf(' ') + 1);
					}
				}
				else
				{
					FirstName = value;
				}
			}
		}
		/// <summary>
		/// Gets or sets the organizations.
		/// </summary>
		/// <value>The organizations.</value>
		public IEnumerable<IOrganization> Organizations
		{
			get { return ServiceModelOrganizations.Select(organization => (IOrganization)organization); }
			set { ServiceModelOrganizations = value.Select(organization => (OrganizationServiceModel)organization); }
		}
		/// <summary>
		/// Gets or sets the organizations.
		/// </summary>
		/// <value>The organizations.</value>
		[DataMember(Name="Organizations")]
		public IEnumerable<OrganizationServiceModel> ServiceModelOrganizations { get; set; }
		/// <summary>
		/// Gets or sets the addresses.
		/// </summary>
		/// <value>The addresses.</value>
		public IEnumerable<IAddress> Addresses
		{
			get { return ServiceModelAddresses.Select(address => (IAddress)address); }
			set { ServiceModelAddresses = value.Select(address => (AddressServiceModel)address); }
		}
		/// <summary>
		/// Gets or sets the addresses.
		/// </summary>
		/// <value>The addresses.</value>
		[DataMember(Name="Addresses")]
		public IEnumerable<AddressServiceModel> ServiceModelAddresses { get; set; }
		/// <summary>
		/// Gets or sets the emails.
		/// </summary>
		/// <value>The emails.</value>
		public IEnumerable<IEmail> Emails
		{
			get { return ServiceModelEmails.Select(email => (IEmail)email); }
			set { ServiceModelEmails = value.Select(email => (EmailServiceModel)email); }
		}
		/// <summary>
		/// Gets or sets the emails.
		/// </summary>
		/// <value>The emails.</value>
		[DataMember(Name="Emails")]
		public IEnumerable<EmailServiceModel> ServiceModelEmails { get; set; }
		/// <summary>
		/// Gets or sets the phones.
		/// </summary>
		/// <value>The phones.</value>
		public IEnumerable<IPhone> Phones
		{
			get { return ServiceModelPhones.Select(phone => (IPhone)phone); }
			set { ServiceModelPhones = value.Select(phone => (PhoneServiceModel)phone); }
		}
		/// <summary>
		/// Gets or sets the phones.
		/// </summary>
		/// <value>The phones.</value>
		[DataMember(Name="Phones")]
		public IEnumerable<PhoneServiceModel> ServiceModelPhones { get; set; }
		/// <summary>
		/// Gets or sets the groups.
		/// </summary>
		/// <value>The groups.</value>
		public IEnumerable<IGroup> Groups
		{
			get { return ServiceModelGroups.OfType<IGroup>(); }
			set { ServiceModelGroups = value.OfType<GroupServiceModel>(); }
		}
		/// <summary>
		/// Gets or sets the groups.
		/// </summary>
		/// <value>The groups.</value>
		[DataMember(Name="Groups")]
		public IEnumerable<GroupServiceModel> ServiceModelGroups { get; set; }
	}
}
